package com.gsbelarus.gedemin.salary.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.model.DayEventModel;
import com.gsbelarus.gedemin.salary.util.ExtractStringHelper;

import io.realm.RealmList;

public class WScheduleRecylerViewAdapter extends RecyclerView.Adapter<WScheduleRecylerViewAdapter.RecyclerItemViewHolder> {

    private RealmList<DayEventModel> dayEventDataSet;

    private Context context;

    public final static class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvEventType;
        TextView tvEventDescription;
        ImageView ivOval;

        public RecyclerItemViewHolder(View itemView) {
            super(itemView);
            ivOval = (ImageView) itemView.findViewById(R.id.ivOval);
            tvEventType = (TextView) itemView.findViewById(R.id.tvEventType);
            tvEventDescription = (TextView) itemView.findViewById(R.id.tvEventDescription);
        }
    }

    public WScheduleRecylerViewAdapter(RealmList<DayEventModel> dayEventDataSet) {
        if (dayEventDataSet == null) {
            //Crashlytics.getInstance().core.logException(new IllegalArgumentException("dayEventDataSet must not be null"));
            throw new IllegalArgumentException("dayEventDataSet must not be null");
        }
        this.dayEventDataSet = dayEventDataSet;
    }

    @Override
    public RecyclerItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.work_schedule_recycler_item, viewGroup, false);
        context = viewGroup.getContext();
        return new RecyclerItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerItemViewHolder viewHolder, int position) {
        DayEventModel dayEvent = dayEventDataSet.get(position);

        String dayEventType = "";
        switch (dayEvent.getEventType()) {
            case DAY_SHIFT:
                dayEventType = context.getResources().getString(R.string.event_type_day_shift);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.md_amber_300));
                break;
            case NIGHT_SHIFT:
                dayEventType = context.getResources().getString(R.string.event_type_night_shift);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.md_indigo_400));
                break;
            case PUBLIC_HOLIDAY:
                dayEventType = context.getResources().getString(R.string.event_type_public_holiday);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.credit_color));
                break;
            case PRIVATE_HOLIDAY:
                dayEventType = context.getResources().getString(R.string.event_type_private_holiday);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.credit_color));
                break;
            case VACATION:
                dayEventType = context.getResources().getString(R.string.event_type_vacation);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.debit_color));
                break;
            case CHANGE_WORK_SCHEDULE:
                dayEventType = context.getResources().getString(R.string.event_type_change_work_schedule);
                ((GradientDrawable) viewHolder.ivOval.getBackground()).setColor(context.getResources().getColor(R.color.md_grey_300));
                break;
        }
        viewHolder.tvEventType.setText(dayEventType);

        String dayEventDuration = "";
        if (dayEvent.getEventType() != DayEventModel.EventType.PUBLIC_HOLIDAY)
            if (dayEvent.isAllDay()) {
                dayEventDuration = context.getResources().getString(R.string.event_duration_title) + ": " + context.getResources().getString(R.string.event_duration_all_day) + ".";
            } else {
                dayEventDuration = ExtractStringHelper.getFormatDurationDayEvent(dayEvent);
                if (dayEventDuration.contains(":")) {
                    dayEventDuration = context.getResources().getString(R.string.event_duration_title) + ": " + dayEventDuration + ".";
                } else {
                    dayEventDuration = context.getResources().getString(R.string.event_duration_title) + ": " + dayEventDuration + context.getResources().getString(R.string.hour_abbr);
                }
            }
        String dayEventDescription = dayEvent.getDescription();

        if (dayEvent.getEventType() != DayEventModel.EventType.CHANGE_WORK_SCHEDULE) {
            if (!dayEventDuration.isEmpty())
                viewHolder.tvEventDescription.setText(dayEventDuration + " " + dayEventDescription);
            else
                viewHolder.tvEventDescription.setText(dayEventDescription);
        } else {
            if (dayEventDescription != null && !dayEventDescription.isEmpty())
                viewHolder.tvEventDescription.setText(dayEventDescription);
            else
                viewHolder.tvEventDescription.setText(dayEventDuration);
        }
    }

    @Override
    public int getItemCount() {
        return dayEventDataSet.size();
    }

    public RealmList<DayEventModel> getDataSet() {
        return dayEventDataSet;
    }

    public void setDataSet(RealmList<DayEventModel> dayEventDataSet) {
        this.dayEventDataSet = dayEventDataSet;
        notifyDataSetChanged();
    }

    public DayEventModel getItem(int position) {
        return dayEventDataSet.get(position);
    }
}